"""
面板显示修复工具 - 用于诊断和修复面板不显示的问题
"""
import sys
import traceback
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget, QLabel, QDesktopWidget
from PyQt5.QtCore import Qt, QTimer

def force_panel_show(panel):
    """强制显示面板的函数，确保面板可见"""
    # 确保面板透明度正确
    panel.setWindowOpacity(1.0)
    
    # 确保面板在屏幕可见区域内
    screen = QDesktopWidget().availableGeometry()
    if panel.x() < 0 or panel.y() < 0 or panel.x() > screen.width() or panel.y() > screen.height():
        panel.move(screen.width() // 4, screen.height() // 4)
    
    # 强制面板可见
    panel.setVisible(True)
    panel.show()
    panel.raise_()
    panel.activateWindow()
    
    # 强制处理事件，确保面板显示命令被执行
    QApplication.processEvents()

class PanelFixTool(QMainWindow):
    """面板显示问题修复工具"""
    
    def __init__(self):
        super().__init__()
        self.setWindowTitle("面板显示修复工具")
        self.setGeometry(100, 100, 400, 500)
        
        # 创建中央部件
        central_widget = QWidget()
        self.setCentralWidget(central_widget)
        
        # 创建布局
        layout = QVBoxLayout(central_widget)
        
        # 创建标签
        self.status_label = QLabel("准备就绪")
        layout.addWidget(self.status_label)
        
        # 创建按钮
        fix_button = QPushButton("修复面板显示问题")
        fix_button.clicked.connect(self.fix_panel_display)
        layout.addWidget(fix_button)
        
        # 强制修复按钮
        force_fix_button = QPushButton("强制重建并显示面板")
        force_fix_button.clicked.connect(self.force_rebuild_panel)
        layout.addWidget(force_fix_button)
        
        # 诊断按钮
        diagnose_button = QPushButton("运行诊断")
        diagnose_button.clicked.connect(self.run_diagnostics)
        layout.addWidget(diagnose_button)
        
        # 应用引用
        self.app_launcher = None
    
    def fix_panel_display(self):
        """修复面板显示问题"""
        try:
            self.status_label.setText("正在修复面板显示问题...")
            
            # 导入应用启动器
            from app_launcher_fluent import AppLauncherFluent
            
            # 创建应用启动器实例（如果尚未创建）
            if not self.app_launcher:
                self.app_launcher = AppLauncherFluent()
            
            # 检查是否存在面板
            if not hasattr(self.app_launcher, 'shortcutsPanel') or self.app_launcher.shortcutsPanel is None:
                self.status_label.setText("面板不存在，正在创建...")
                from app_launcher_fluent import ShortcutsPanel
                self.app_launcher.shortcutsPanel = ShortcutsPanel()
                
            # 直接显示面板
            panel = self.app_launcher.shortcutsPanel
            force_panel_show(panel)
            
            self.status_label.setText("面板已显示，检查是否可见")
        except Exception as e:
            self.status_label.setText(f"修复过程中出错: {e}")
            traceback.print_exc()
    
    def force_rebuild_panel(self):
        """强制重建面板并显示"""
        try:
            self.status_label.setText("强制重建并显示面板...")
            
            # 导入应用启动器
            from app_launcher_fluent import AppLauncherFluent, ShortcutsPanel
            
            # 创建或获取应用启动器实例
            if not self.app_launcher:
                self.app_launcher = AppLauncherFluent()
            
            # 删除现有面板（如果存在）
            if hasattr(self.app_launcher, 'shortcutsPanel') and self.app_launcher.shortcutsPanel:
                try:
                    old_panel = self.app_launcher.shortcutsPanel
                    old_panel.hide()
                    old_panel.deleteLater()
                except:
                    pass
                
            # 创建新面板
            self.app_launcher.shortcutsPanel = ShortcutsPanel()
            panel = self.app_launcher.shortcutsPanel
            
            # 移动到屏幕中央
            screen = QDesktopWidget().availableGeometry()
            panel.move(screen.width() // 4, screen.height() // 4)
            
            # 强制显示
            force_panel_show(panel)
            
            self.status_label.setText("面板已重建并显示")
        except Exception as e:
            self.status_label.setText(f"重建面板过程中出错: {e}")
            traceback.print_exc()
    
    def run_diagnostics(self):
        """运行诊断"""
        try:
            self.status_label.setText("正在诊断问题...")
            
            # 检查基本环境
            self.status_label.setText("1. 检查Python环境...")
            print(f"Python版本: {sys.version}")
            
            # 检查PyQt版本
            self.status_label.setText("2. 检查PyQt版本...")
            from PyQt5.QtCore import QT_VERSION_STR
            print(f"Qt版本: {QT_VERSION_STR}")
            
            # 检查屏幕信息
            self.status_label.setText("3. 检查屏幕信息...")
            screen = QDesktopWidget().availableGeometry()
            print(f"屏幕尺寸: {screen.width()}x{screen.height()}")
            
            # 检查窗口可见性处理
            self.status_label.setText("4. 测试基本窗口显示...")
            test_window = QMainWindow()
            test_window.setWindowTitle("测试窗口")
            test_window.resize(300, 200)
            test_window.show()
            QTimer.singleShot(2000, test_window.hide)
            
            self.status_label.setText("诊断完成，请查看控制台输出")
        except Exception as e:
            self.status_label.setText(f"诊断过程中出错: {e}")
            traceback.print_exc()
            
if __name__ == "__main__":
    app = QApplication(sys.argv)
    fix_tool = PanelFixTool()
    fix_tool.show()
    sys.exit(app.exec_())
